package com.uniriho.lxr9100ble.service;

import android.annotation.SuppressLint;

import yc.bluetooth.androidble.ble.BLEManager;

public class BluetoolUtil {

    private final static String TAG = "BluetoolUtil";

    @SuppressLint("StaticFieldLeak")
    public static BluetoolUtil instance;

    private BluetoolUtil() {
        initlib();
    }

    public static BluetoolUtil getInstance() {
        if (instance == null) {
            instance = new BluetoolUtil();
        }
        return instance;
    }


    static {
        try {
            System.loadLibrary("lxr9100ble");
        } catch (UnsatisfiedLinkError ule) {
            ule.printStackTrace();
            System.err.println("WARNING: Could not load serial_jni library!");
        }
    }

    /**
     * 初始化环境变量
     */
    public native int initlib();

//    private byte[] myData = new byte[]{1, 2, 3, 4};


    /**
     * 发送蓝牙数据
     *
     * @return 0：发送成功   -1：蓝牙未连接    -2：
     */
    public int sendBleData(byte[] sendData,int sendLen) {
//        byte[] mSend = new byte[]{(byte) 0xbb,0x66,0x00,0x04,0x00,0x00,0x04};
        return BLEManager.getInstance().sendMessage(sendData, sendLen);
    }

    /**
     * 返回读的数据
     *
     * @param recvData 出口参数：接受数据
     * @param readLen  出口参数：接受数据长度
     * @return 读取结果  0：成功  -1：读取失败（数据为空）
     */
    public int readBleData(byte[] recvData, int readLen) {

        return  BLEManager.getInstance().readBleData(recvData,readLen);

    }

}
